package cn.elead.chaos.sso.server.web.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.sso.server.web.system.domain.SsoClient;
import cn.elead.chaos.sso.server.web.system.service.ISsoClientService;

/**
 * 客户端信息Controller
 * 
 * @author luopeng
 * @date 2020-01-16
 */
@Controller
@RequestMapping("/system/client")
public class SsoClientController extends BaseController {
	private String prefix = "system/client";

	@Autowired
	private ISsoClientService ssoClientService;

	@GetMapping
	@RequiresPermissions("system:client:view")
	public String client() {
		return prefix + "/client";
	}

	/**
	 * 查询客户端信息列表
	 */
	@ResponseBody
	@UnifiedReturn
	@PostMapping("/list")
	@RequiresPermissions("system:client:list")
	public TableDataInfo<SsoClient> list(SsoClient ssoClient) {
		startPage();
		List<SsoClient> list = ssoClientService.selectSsoClientList(ssoClient);
		return getDataTable(list);
	}

	/**
	 * 导出客户端信息列表
	 */
	@ResponseBody
	@PostMapping("/export")
	@RequiresPermissions("system:client:export")
	@Log(title = "客户端信息", businessType = BusinessType.EXPORT)
	public String export(SsoClient ssoClient) {
		List<SsoClient> list = ssoClientService.selectSsoClientList(ssoClient);
		ExcelUtil<SsoClient> util = new ExcelUtil<SsoClient>(SsoClient.class);
		return util.exportExcel(list, "client");
	}

	/**
	 * 新增客户端信息
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存客户端信息
	 */
	@ResponseBody
	@PostMapping("/add")
	@RequiresPermissions("system:client:add")
	@Log(title = "客户端信息", businessType = BusinessType.INSERT)
	public void addSave(SsoClient ssoClient) {
		ssoClientService.insertSsoClient(ssoClient);
	}

	/**
	 * 修改客户端信息
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		SsoClient ssoClient = ssoClientService.selectSsoClientById(id);
		mmap.put("ssoClient", ssoClient);
		return prefix + "/edit";
	}

	/**
	 * 修改保存客户端信息
	 */
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("system:client:edit")
	@Log(title = "客户端信息", businessType = BusinessType.UPDATE)
	public void editSave(SsoClient ssoClient) {
		ssoClientService.updateSsoClient(ssoClient);
	}

	/**
	 * 删除客户端信息
	 */
	@ResponseBody
	@PostMapping("/remove")
	@RequiresPermissions("system:client:remove")
	@Log(title = "客户端信息", businessType = BusinessType.DELETE)
	public void remove(String ids) {
		ssoClientService.deleteSsoClientByIds(ids);
	}
}
